package wang.zl.bookmanager1.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.handlers.GsonTypeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import wang.zl.bookmanager1.R.ResultInfo;
import wang.zl.bookmanager1.domain.Book;
import wang.zl.bookmanager1.domain.Borrow_detail;
import wang.zl.bookmanager1.domain.User;
import wang.zl.bookmanager1.service.impl.BookServiceImpl;
import wang.zl.bookmanager1.service.impl.Borrow_detailServiceImpl;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import static javax.swing.text.html.CSS.getAttribute;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 黑马程序员
 * @since 2022-12-07
 */
@RestController
@RequestMapping("/borrow_detail")
public class Borrow_detailController {
    @Autowired
    BookServiceImpl bookService;
    @Autowired
    Borrow_detailServiceImpl borrow;
    @GetMapping ("book")
    @Transactional
    public ResultInfo book(int id, HttpSession session){
            //查看是否起售
            Book book = bookService.getById(id);
            Integer status = book.getStatus();
            if(status == 0){
                return new ResultInfo(false,null,"已经停售了");
            }
            Integer number = book.getNumber();
            if ( number<=0) {
                return new ResultInfo(false, null, "没有存货了哦，请下次再来");
            }
            //查看是否借阅此书了
        LambdaQueryWrapper<Borrow_detail> qw = new LambdaQueryWrapper<>();
        qw.eq(Borrow_detail::getBookId, book.getId()).eq(Borrow_detail::getUserId, ((User) session.getAttribute("user")).getId());
        Borrow_detail one = borrow.getOne(qw);
        if (one != null){
            if(one.getStatus()==0){
                return new ResultInfo(false,null,"你已经借过书了");
            }
            one.setStatus(0);
            borrow.updateById(one);
            return new ResultInfo(true,null,"成功");
        }

        book.setNumber(number-1);
        bookService.updateById(book);
        Borrow_detail bd= new Borrow_detail();
        bd.setBookId(book.getId());
        bd.setStatus(0);
        borrow.save(bd);
        return new ResultInfo(true,null,"成功");
    }

    @PostMapping("findAlldb")
    public ResultInfo findAlldb (HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Borrow_detail>  list = new ArrayList<>();
        LambdaQueryWrapper<Borrow_detail> wrapper = new LambdaQueryWrapper<>();;
        if(user.getRole() == 1){
            wrapper.eq(Borrow_detail::getUserId,user.getId());
            list = borrow.list(wrapper);
        }else {
             list =  borrow.list();
        }
        if(list.size()!=0){
            return new ResultInfo(true,list,"成功");
        }else {
            return new ResultInfo(false,null,"暂无数据");
        }
    }

    @GetMapping("reBook")
    @Transactional
    public ResultInfo reBook(Long id,String bookName){
        Borrow_detail bd1 = new Borrow_detail();
        bd1.setId(id);
        bd1.setStatus(1);
        borrow.updateById(bd1);
        LambdaQueryWrapper<Book> qw = new LambdaQueryWrapper<>();

      //更改书籍数目
        qw.eq(Book::getBookName,bookName);
        Book one = bookService.getOne(qw);
        one.setBookName(bookName);
        one.setNumber(one.getNumber()+1);
        bookService.updateById(one);

        return new ResultInfo(true,null,"还书成功");
    }


}

